package com.bilibili.java.books.servlet;


import java.io.IOException;

import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.java.books.bean.User;
import com.bilibili.java.books.service.UserService;
import com.bilibili.java.books.service.impl.UserServiceImpl;

//@WebServlet(value= {"/accountLongin","/phoneLongin","/phoneVerify","/queryUser","queryPhone"})
public class BooksServlet extends HttpServlet{
	
	UserService service=new UserServiceImpl();

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
		
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取Url-pattern路径
		String method=req.getServletPath();
		
		//截取路径名 substring方法第一个参数代表从字符串什么下标位开始,第二个参数代表截取的长度,如不写第二个，默认为字符串的长度
		method=method.substring(1, method.length());
		
		Method m=null;
		
		try {
			
			//获取当前类中名字为 method 的方法
			//getDeclaredMethod(String method)在反射中获取类中和传入的method相同的方法，可以是声明的所有类型的方法
			m=this.getClass().getDeclaredMethod(method, HttpServletRequest.class,HttpServletResponse.class);
			
			//调用获得的方法
			m.invoke(this, req,resp);
			
			
		} catch (Exception e) {
		
			e.printStackTrace();
		} 
		
	}
	/**
	 * 账号登陆的方法
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	private void accountLongin(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		String account=req.getParameter("account");
		String pwd=req.getParameter("pwd");
		
		User user=new User(account,pwd);
		
		String state=service.userLogin(user);
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		
		 if(state.equals("登录成功")){
			 
			User u=service.getUser(user);
		
			req.getSession().setAttribute("user", u);
			//判断是否要10天免登录
			if(req.getParameter("avoidlanding")!=null) {
				//定义一个Cookie对象来存放登陆的账户信息
				Cookie accountCookie=new Cookie("accountCookie",u.getAccount());
				//设置Cookie的持久化存放时间
				accountCookie.setMaxAge(3600*240);
				//设置Cookie的作用范围
				accountCookie.setPath("/");
				
				//把Cookie对象添加到响应中
				resp.addCookie(accountCookie);
				
				
				Cookie pwdCookie=new Cookie("pwdCookie",u.getPwd());
				
				pwdCookie.setMaxAge(60*60*24*10);
				
				pwdCookie.setPath("/");
				
				resp.addCookie(pwdCookie);
			}
				
			
			
			 resp.getWriter().print("<script>alert('"+state+"!');window.location.href='/J54Books/accessHomePage'</script>");
			
	         
	           
	      }else{
	    	  
	    	  Cookie[] cookies=req.getCookies();
	    	  
	    	  if(cookies!=null&&cookies.length>0) {
	    		  
	    		  for (Cookie cookie : cookies) {
					
	    			  if(cookie.getName().equals("pwdCookie")) {
	    				  cookie.setMaxAge(0);
	    				  cookie.setPath("/");
	    				  resp.addCookie(cookie);
	    				  break;
	    			  }
				}
	    	  }
	    	  
	    	  
	    	  
	                //登录不成功返回重新登录  
	    	  resp.getWriter().print("<script>alert('"+state+"!');window.location.href='"+req.getServletContext().getContextPath()+"/J54Books/jsp/login.jsp'</script>");
	    	  
	              //  resp.sendRedirect(req.getServletContext().getContextPath() + "/J54Books/html/login.html");
	      }
	
	}
	/**
	 * 手机登录的方法
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void phoneLongin(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		String phone=req.getParameter("phone");
		String code=req.getParameter("code");
		User user=new User();
		user.setPhone(phone);
		user.setVeriCode(code);
		
		String state=service.phoneQuery(user);

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		
		 if(state.equals("登录成功")){
			 
			 User u=service.getUser(user);
				
			req.getSession().setAttribute("user", u);
			 
			
			 resp.getWriter().print("<script>alert('"+state+"!');window.location.href='/J54Books/accessHomePage'</script>");
	           // resp.sendRedirect("/J54Books/accessHomePage");
	           
	      }else{
	                //登录不成功返回重新登录  
	    	  resp.getWriter().print("<script>alert('"+state+"!');window.location.href='"+req.getServletContext().getContextPath()+"/J54Books/jsp/login.jsp'</script>");
	    	  
	              //  resp.sendRedirect(req.getServletContext().getContextPath() + "/J54Books/html/login.html");
	      }
	
		
		
	}
	/**
	 * 手机验证码发送
	 */
	private void phoneVerify(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		
		String phone=req.getParameter("phone");
		
		String state=service.phoneVerify(phone);
		

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		resp.getWriter().print(state);
		
		
		
	}
	
	/**
	 * 账户名查询账户是否存在
	 */
	private void queryUser(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String account=req.getParameter("account");
		
		String state=service.queryUser(account);
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		resp.getWriter().print(state);
	
		
	}
	/**
	 * 查询手机是否存在
	 */
	public void queryPhone(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF-8");
		String phone=req.getParameter("phone");
		
		String state=service.queryPhone(phone);

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		resp.getWriter().print(state);
		
	}
	/**
	 * 账户注册
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void userRegister(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		String account=req.getParameter("account");
		String phone=req.getParameter("phone");
		String nickname=req.getParameter("nickname");
		
		String pwd=req.getParameter("pwd");
		
		User user=new User();
		user.setAccount(account);
		user.setPwd(pwd);
		user.setPhone(phone);
		user.setNickname(nickname);
		
		String state=service.userRegister(user);
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		
		if(state.equals("注册成功")) {
			User u=service.getUser(user);
			
			req.getSession().setAttribute("user", u);
			
			
			 resp.getWriter().print("<script>alert('"+state+"!');window.location.href='/J54Books/accessHomePage'</script>");
			
			//resp.sendRedirect("/J54Books/J54Books/html/login.html");
		}else {
			 resp.getWriter().print("<script>alert('"+state+"!');window.location.href='/J54Books/J54Books/jsp/register.jsp'</script>");
			
			//resp.sendRedirect("/J54Books/J54Books/html/register.html");
			
		}
		
	}
	
	
}
